package ar.edu.unlp.info.cptn.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import ar.edu.unlp.info.cptn.model.Persona;

public class PersonaValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return Persona.class.equals(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Persona persona = (Persona) arg0;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "nombre", "error.field.required", "Required field");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "apellido", "error.field.required", "Required field");
		if ( ! errors.hasFieldErrors("nombre")) {
			if (persona.getNombre().length() > 10){
				errors.rejectValue("nombre", "error.field.long", "Too long");
			}
		}

	}

}
